﻿using Cofe.Core.Entry;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cofe.Core.Actions
{

    [Obsolete("Use LinkEntryTransfer, this is removed.")]
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class LinkEntryToCustomEntryList : ITransferHelper
    {
        public TransferMode GetSupportedMode(ICofeSystemInfo[] entries, ICofeDirectoryInfo fromm, ICofeDirectoryInfo too)
        {
            TransferMode retVal = TransferMode.None;
            
            if (entries == null || entries.Length == 0)                         
                return TransferMode.None;            
            
            if (!(too.Is<ICustomEntryList>()) && !(too.Properties.PrimaryPropertyProvider is ICustomEntryListPropertyProvider))                           
                return TransferMode.None;

            if (entries.Any(e => e.Is<IEntryLink>()))
                return retVal |= TransferMode.Copy; //Copy links.             
            else retVal |= TransferMode.Link;

            return retVal;
        }

        public async Task<TransferMode> TransferAsync(TransferMode mode, ICofeSystemInfo[] entries, ICofeDirectoryInfo fromm,
            ICofeDirectoryInfo too, IProgress<ProgressEventArgs> progress, CancellationToken cts)
        {
            if (entries == null)
                throw new ArgumentException("entry");

            if (too == null)
                throw new ArgumentException("too");

            await too.Properties.ConnectAsync();
            if (mode == TransferMode.Link && GetSupportedMode(entries, fromm, too) == TransferMode.Link)
            {
                var customPP = too.Properties.AllPropertyProviders.First(p => p is ICustomEntryListPropertyProvider)
                       as ICustomEntryListPropertyProvider;
                foreach (var entry in entries)
                {
                    customPP.add(entry.Name, null, entry.Properties);
                }
                return TransferMode.Link;
            }

            if (mode == TransferMode.Copy && GetSupportedMode(entries, fromm, too) == TransferMode.Copy)
            {
                var customPP = too.Properties.AllPropertyProviders.First(p => p is ICustomEntryListPropertyProvider)
                       as ICustomEntryListPropertyProvider;
                foreach (var entry in entries)
                    if (entry.Is<IEntryLink>())
                    {
                        customPP.add(entry.Name, null, entry.As<IEntryLink>().Entry.Properties);
                    }
                return TransferMode.Link;
            }

            return TransferMode.None;

        }

        public Task<TransferMode> RenameAsync(TransferMode mode, ICofeSystemInfo entry, ICofeDirectoryInfo fromm,
         string newName, IProgress<ProgressEventArgs> progress, CancellationToken cts)
        {
            throw new NotImplementedException();
        }


        private IEntryConstructor _entryConstructor = null;
        public ITransferHelper[] _allHelpers = null;

        public IEntryConstructor EntryConstructor
        {
            get { return _entryConstructor ?? CofeServices.ServiceLocater.FindService<IEntryConstructor>(); }
            set { _entryConstructor = value; }
        }
        public ITransferHelper[] AllHelpers
        {
            get { return _allHelpers ?? CofeServices.ServiceLocater.FindAllServices<ITransferHelper>().ToArray(); }
            set { _allHelpers = value; }
        }
    }
}
